package org.erp.store.infrastructure.config;

import org.erp.common.enums.DeviceInventoryEvent;
import org.erp.store.infrastructure.enums.DeviceInventoryState;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;




/**
 * 设备库存状态机配置
 */
@Configuration
@EnableStateMachineFactory
public class DeviceInventoryStateMachineConfig extends EnumStateMachineConfigurerAdapter<DeviceInventoryState, DeviceInventoryEvent> {

    @Override
    public void configure(StateMachineConfigurationConfigurer<DeviceInventoryState, DeviceInventoryEvent> config) throws Exception {
        config
            .withConfiguration()
            .autoStartup(false);
    }

    @Override
    public void configure(StateMachineStateConfigurer<DeviceInventoryState, DeviceInventoryEvent> states) throws Exception {
        states
            .withStates()
            .initial(DeviceInventoryState.INBOUND)
            .states(EnumSet.allOf(DeviceInventoryState.class));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<DeviceInventoryState, DeviceInventoryEvent> transitions) throws Exception {
        transitions
            // 入库 -> 调拨中
            .withExternal().source(DeviceInventoryState.INBOUND).target(DeviceInventoryState.TRANSFERRING).event(DeviceInventoryEvent.ALLOCATE)
            .and()
            // 调拨中 -> 接驳
            .withExternal().source(DeviceInventoryState.TRANSFERRING).target(DeviceInventoryState.HANDOVER).event(DeviceInventoryEvent.HANDOVER)
            .and()
            // 接驳 -> 售出
            .withExternal().source(DeviceInventoryState.HANDOVER).target(DeviceInventoryState.SOLD).event(DeviceInventoryEvent.SELL)
            .and()
            // 调拨中 -> 驳回
            .withExternal().source(DeviceInventoryState.TRANSFERRING).target(DeviceInventoryState.REJECTED).event(DeviceInventoryEvent.REJECT)
            .and()
            // 驳回 -> 入库
            .withExternal().source(DeviceInventoryState.REJECTED).target(DeviceInventoryState.INBOUND).event(DeviceInventoryEvent.REJECT_TO_INBOUND)
            .and()
            // 接驳 -> 待返库
            .withExternal().source(DeviceInventoryState.HANDOVER).target(DeviceInventoryState.WAIT_RETURN).event(DeviceInventoryEvent.REQUEST_RETURN)
            .and()
            // 待返库 -> 入库
            .withExternal().source(DeviceInventoryState.WAIT_RETURN).target(DeviceInventoryState.INBOUND).event(DeviceInventoryEvent.RETURN_TO_INBOUND)
            .and()
            // 接驳 -> 待退库
            .withExternal().source(DeviceInventoryState.HANDOVER).target(DeviceInventoryState.WAIT_REFUND).event(DeviceInventoryEvent.REQUEST_REFUND)
            .and()
            // 待退库 -> 已退库
            .withExternal().source(DeviceInventoryState.WAIT_REFUND).target(DeviceInventoryState.REFUNDED).event(DeviceInventoryEvent.CONFIRM_REFUND)
            .and()
            // 驳回 -> 调拨中（撤销驳回）
            .withExternal().source(DeviceInventoryState.REJECTED).target(DeviceInventoryState.TRANSFERRING).event(DeviceInventoryEvent.CANCEL_REJECT);
    }
}